package scau.zxck.view.node;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import scau.zxck.constant.PATH;
import scau.zxck.entity.Connection;
import scau.zxck.utils.PathUtil;
import scau.zxck.utils.ViewUtil;
import scau.zxck.view.Main;
import scau.zxck.view.stage.AlertStage;
import java.io.IOException;

/**
 * Created by chenyulin on 2016/11/29.
 * 自定义的连接树菜单
 */
public class ConnTreeContextMenu extends ContextMenu {

    public static MenuItem miConnect = new MenuItem("Connect");
    public static MenuItem miClose = new MenuItem("Close Connection");
    public static MenuItem miNew = new MenuItem("Add Connection");
    public static MenuItem miEdit = new MenuItem("Edit");
    public static MenuItem miDelete = new MenuItem("Delete");

    public ConnTreeContextMenu() {
        SeparatorMenuItem line1 = new SeparatorMenuItem();
        SeparatorMenuItem line2 = new SeparatorMenuItem();
        this.getItems().addAll(miConnect, miClose, line1, miEdit, miDelete, line2, miNew);
        // 连接
        miConnect.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                int connIndex = Main.connTreeController.getSelectedItem().getConnIndex();
                Connection conn = Main.connectionList.get(connIndex);
                conn.initDatabaseList();
                Main.connTreeController.initialize();
            }
        });
        // 关闭连接
        miClose.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                MyTreeItem selectedItem = Main.connTreeController.getSelectedItem();
                int connIndex = selectedItem.getConnIndex();
                Connection conn = Main.connectionList.get(connIndex);
                conn.getDatabaseList().clear();
                conn.setConnected(false);
                Main.connTreeController.initialize();
            }
        });
        // 新建
        miNew.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                Main.newConnStage = new Stage();
                Parent root = null;
                try {
                    FXMLLoader newLoader = new FXMLLoader(PathUtil.getResource(PATH.FXML_CREATE_CONN_PATH));
                    root = newLoader.load();
                } catch (IOException e) {
                    new AlertStage("Error", "Failed to load fxml").show();
                    e.printStackTrace();
                }
                Main.newConnStage.setTitle("New Connection");
                Main.newConnStage.getIcons().add((new Image("image/create.png")));
                Main.newConnStage.setScene(new Scene(root, 270, 370));
                Main.newConnStage.setResizable(false);
                Main.newConnStage.setAlwaysOnTop(true);
                Main.newConnStage.show();
            }
        });
        // 编辑
        miEdit.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                MyTreeItem selectedItem = Main.connTreeController.getSelectedItem();
                int connIndex = selectedItem.getConnIndex();
                int dbIndex = selectedItem.getDatabaseIndex();
                if (connIndex != -1 && dbIndex == -1) {
                    Main.configConnStage = new Stage();
                    Parent root = null;
                    try {
                        FXMLLoader newLoader = new FXMLLoader(PathUtil.getResource(PATH.FXML_EDIT_CONN_PATH));
                        root = newLoader.load();
                    } catch (IOException e) {
                        new AlertStage("Error", "Failed to load fxml").show();
                        e.printStackTrace();
                    }
                    Main.configConnStage.setTitle("Connection Edit");
                    Main.configConnStage.setScene(new Scene(root, 270, 370));
                    Main.configConnStage.setResizable(false);
                    Main.configConnStage.getIcons().add((new Image("image/zxck_logo.png")));
                    Main.configConnStage.show();
                } else
                    new AlertStage("Please choose a connection").show();
            }
        });
        // 删除
        miDelete.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent actionEvent) {
                MyTreeItem selectedItem = Main.connTreeController.getSelectedItem();
                int connIndex = selectedItem.getConnIndex();
                int dbIndex = selectedItem.getDatabaseIndex();
                if (connIndex != -1 && dbIndex == -1) {
                    Main.connectionList.remove(connIndex);
                    Main.connTreeController.initialize();
                    ViewUtil.writeInfoToConfigFile();
                }
                else
                    new AlertStage("Please choose a connection").show();
            }
        });
    }
}
